<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="25%" :title="`${drawerProps.title}-系统配置表`">
    <el-form ref="ruleFormRef" label-width="100px" label-suffix=":" :rules="rules" :model="operateDataInfo">
      <el-form-item label="配置key" prop="configKey">
        <el-input v-model="operateDataInfo.configKey" placeholder="请输入配置key" clearable></el-input>
      </el-form-item>
      <el-form-item label="配置value" prop="configValue">
        <el-input v-model="operateDataInfo.configValue" type="textarea" placeholder="请输入配置value" clearable></el-input>
      </el-form-item>
      <el-form-item label="分类" prop="category">
        <el-select v-model="operateDataInfo.category" placeholder="请选择分类">
          <el-option
            v-for="item in drawerProps.enumMap?.get('category')"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input v-model="operateDataInfo.remark" placeholder="请输入配置描述" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { getSysConfigInfo, SysConfigPageResult, SysConfigSaveParam, SysConfigUpdateParam } from "@/api/modules/sys/SysConfig";

interface DrawerProps {
  title: string;
  isSave: boolean;
  isUpdate: boolean;
  row: Partial<SysConfigPageResult>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
  enumMap?: Map<string, { [key: string]: any }[]>;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  title: "",
  isSave: false,
  isUpdate: false,
  row: {}
});

const rules = reactive({
  // 规则
  configKey: [{ required: true, message: "请填写配置key" }],
  category: [{ required: true, message: "请选择分类" }]
});

const operateDataInfo = ref<SysConfigSaveParam | SysConfigUpdateParam>({});
const openDrawer = async (params: DrawerProps) => {
  if (params.isUpdate) {
    // 更新信息，先查询
    let { data } = await getSysConfigInfo(params.row.id ? params.row.id : "");
    operateDataInfo.value = { ...data };
  } else if (params.isSave) {
    // 新增前的默认值
    operateDataInfo.value = {};
  }
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!(operateDataInfo.value);
      ElMessage.success({ message: `${drawerProps.value.title} 成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  openDrawer
});
</script>

<style scoped lang="scss"></style>
